import router from '../router'
import store from '../store'

const permission = router.beforeEach((to, from, next) => {
  const token = store.getters.token
  if (!token) {
    if (to.path === '/login') {
      next()
    } else {
      store.dispatch('user/getInfo', localStorage.authorization).then(resp => {
        if (resp.success) {
          next()
        } else {
          next(`/login?redirect=${to.path}`)
        }
      })
    }
  } else {
    next()
  }
})
export default permission
